package cn.myframe.redis;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class DistributedRedisLock {
	
	/*@Autowired
	RedissonClient redission;
	private static final String LOCK_TITLE = "redisLock_";
	
	public void acquire(String lockName){
        String key = LOCK_TITLE + lockName;
        RLock mylock = redission.getFairLock(key);
        mylock.lock(30, TimeUnit.SECONDS); //lock提供带timeout参数，timeout结束强制解锁，防止死锁
      //  System.err.println("======lock======"+Thread.currentThread().getName());
    }

    public  void release(String lockName){
        String key = LOCK_TITLE + lockName;
        RLock mylock = redission.getLock(key);
        mylock.unlock();
      //  System.err.println("======unlock======"+Thread.currentThread().getName());
    }
    
    public boolean tryLock(String lockName){
    	String key = LOCK_TITLE + lockName;
    	RLock mylock = redission.getLock(key);
    	try {
    		while(mylock.tryLock(30, 30, TimeUnit.SECONDS)){
        		return true;
        	}
		}catch(Exception e){
		}
    	System.err.println("********ON lock********"+Thread.currentThread().getName());
    	return false;
    }
    
    public boolean tryLock(String lockName,long waitTime,long leaseTime){
    	String key = LOCK_TITLE + lockName;
    	RLock mylock = redission.getLock(key);
    	try {
    		while(mylock.tryLock(waitTime,leaseTime,TimeUnit.MILLISECONDS )){
        		return true;
        	}
		}catch(Exception e){
		}
    	log.error("Thread is no get lock[{}]!"+Thread.currentThread().getName(),lockName);
    	return false;
    }*/

}
